package com.example.yrdev.ordering_system.CustomView;


import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;

import com.example.yrdev.ordering_system.R;
import com.example.yrdev.ordering_system.Utils.GlobalFunction;

import java.sql.Time;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;


import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

/**
 * Created by yrdev on 8/29/16.
 */
public class TimeDatePicker extends Fragment implements View.OnClickListener{
    String title;


    Context context;

    Callback callback;
 public Boolean showTime=true;

    public interface Callback{
        void getDate(Date date);

    }

    public void setShowTime(Boolean showTime) {

        this.showTime = showTime;

        if (!showTime){
            DateFormat df=new SimpleDateFormat("yyyy年MM月dd日");
            String time=df.format(Calendar.getInstance().getTime());
            textView.setText(time);
        }

    }

    public void setCallback(Callback callback) {
        this.callback = callback;
    }

    public TimeDatePicker(){

    }


    public TimeDatePicker(Context context,String title){

        this.context=context;
        this.title=title;

    }

    public TimeDatePicker(Context context,String title,Boolean showTime){

        this.context=context;
        this.title=title;
        this.showTime=showTime;

    }

    TextView textView;

    @Nullable


String timeFormatDefault="yyyy年MM月dd日, HH:mm";

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        LinearLayout linearLayout=new LinearLayout(context);
        linearLayout.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));

       textView=new TextView(context);
        if (!showTime){
            timeFormatDefault="yyyy年MM月dd日";
        }





        DateFormat df=new SimpleDateFormat(timeFormatDefault, Locale.getDefault());

        String time=df.format(Calendar.getInstance().getTime());

        textView.setText(time);
        textView.setTextColor(ContextCompat.getColor(context,R.color.colorPrimary));
        linearLayout.addView(textView);
        linearLayout.setOnClickListener(this);
        linearLayout.setGravity(Gravity.CENTER);
       return linearLayout;


    }

    @Override
    public void onClick(View view) {
        TimeDatePickerView timeDatePickerView=new TimeDatePickerView(context,title,showTime);
        timeDatePickerView.show();
        timeDatePickerView.setCallback(new TimeDatePickerView.Callback() {
            @Override
            public void getTime(Date date) {
                DateFormat df=new SimpleDateFormat(timeFormatDefault,Locale.getDefault());
                if (callback!=null)
                    callback.getDate(date);

                String time=df.format(date);
                textView.setText(time);
            }
        });

    }

public static class TimeDatePickerView extends Dialog{

String title;
    Callback callback;
    Boolean showTime=true;
  interface Callback{
     void getTime(Date date);

  }
    public TimeDatePickerView(Context context,String title){
        super(context);
        this.title=title;

        requestWindowFeature(Window.FEATURE_NO_TITLE);
    }

    public TimeDatePickerView(Context context,String title,Boolean showTime){
        super(context);
        this.title=title;
        this.showTime=showTime;
        requestWindowFeature(Window.FEATURE_NO_TITLE);
    }


    public void setCallback(Callback callback) {
        this.callback = callback;
    }

    Calendar date;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        date=Calendar.getInstance();


        initView();


    }
    @InjectView(R.id.timeLayout)RelativeLayout timeLayout;

    public void initView(){

        setContentView(R.layout.time_date_picker_layout);

        ButterKnife.inject(this);

        if (!showTime){

            timeLayout.setVisibility(View.GONE);
        }


        TextView dialog_title=(TextView)findViewById(R.id.textView8);
        dialog_title.setText(title);

        Button conf_but=(Button)findViewById(R.id.conf_but);
        conf_but.setOnClickListener(confListener());

        DatePicker datePicker=(DatePicker)findViewById(R.id.dpPicker);
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        datePicker.init(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH), new DatePicker.OnDateChangedListener() {

            @Override
            public void onDateChanged(DatePicker datePicker, int year, int month, int dayOfMonth) {
                Log.d("Date", "Year=" + year + " Month=" + (month + 1) + " day=" + dayOfMonth);
                date.set(year,month,dayOfMonth);
            }
        });

        TimePicker timePicker=(TimePicker)findViewById(R.id.tpPicker);
        timePicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
            @Override
            public void onTimeChanged(TimePicker timePicker, int hourOfDay, int min) {
            GlobalFunction.printText("time",String.valueOf(hourOfDay)+":"+String.valueOf(min));
               date.set(Calendar.HOUR_OF_DAY, hourOfDay);
                date.set(Calendar.MINUTE,min);
            }
        });

    }
   public View.OnClickListener confListener(){
       View.OnClickListener listener=new View.OnClickListener() {
           @Override
           public void onClick(View view) {
               if (callback!=null)

                   callback.getTime(date.getTime());
               dismiss();
           }
       };

               return listener;

   }
}


}

